#!/usr/bin/python
# -*- coding: UTF-8 -*-

from email.mime.text import MIMEText
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import smtplib


#Yandex邮箱smtp服务器
host_server = 'smtp.yandex.com'
#Yandex邮箱smtp服务器端口号
ssl_port = '465'
#发件人用户名
user = 'zabbix@mail.cc.com'
#发件人密码
pwd = 'cc'
#发件人的邮箱
sender_mail = 'zabbix@mail.cc.com'



def YandexsendMail(mail_title,mail_content,receiver):
    # ssl登录
    smtp = smtplib.SMTP(host_server)
    smtp = smtplib.SMTP_SSL(host_server, ssl_port)
    # set_debuglevel()是用来调试的。参数值为1表示开启调试模式，参数值为0关闭调试模式
    smtp.set_debuglevel(1)
    smtp.ehlo(host_server)
    smtp.login(user, pwd)

    msg = MIMEText(mail_content, "plain", 'utf-8')
    msg["Subject"] = Header(mail_title, 'utf-8')
    msg["From"] = sender_mail
    msg["To"] = receiver
    smtp.sendmail(sender_mail, receiver, msg.as_string())
    smtp.quit()

def YandexsendMailWithAttach(mail_title,mail_content,receiver,attachFiles):
    # ssl登录
    smtp = smtplib.SMTP(host_server)
    smtp = smtplib.SMTP_SSL(host_server, ssl_port)
    # set_debuglevel()是用来调试的。参数值为1表示开启调试模式，参数值为0关闭调试模式
    smtp.set_debuglevel(1)
    smtp.ehlo(host_server)
    smtp.login(user, pwd)

    msgRoot = MIMEMultipart()

    msgRoot["Subject"] = Header(mail_title, 'utf-8')
    msgRoot["From"] = sender_mail
    msgRoot["To"] = receiver

    ## 添加文本部分
    msgContent = MIMEText(mail_content, "plain", 'utf-8')
    msgRoot.attach(msgContent)

    ##添加附件部分
    for path in attachFiles:
        if ".jpg" in path:
            # jpg类型附件
            jpg_name = path.split("\\")[-1]
            part = MIMEApplication(open(path, 'rb').read())
            part.add_header('Content-Disposition', 'attachment', filename=jpg_name)
            msgRoot.attach(part)

        if ".pdf" in path:
            # pdf类型附件
            pdf_name = path.split("\\")[-1]
            part = MIMEApplication(open(path, 'rb').read())
            part.add_header('Content-Disposition', 'attachment', filename=pdf_name)
            msgRoot.attach(part)

        if ".xlsx" in path:
            # xlsx类型附件
            xlsx_name = path.split("\\")[-1]
            part = MIMEApplication(open(path, 'rb').read())
            part.add_header('Content-Disposition', 'attachment', filename=xlsx_name)
            msgRoot.attach(part)

        if ".txt" in path:
            # txt类型附件
            txt_name = path.split("\\")[-1]
            part = MIMEApplication(open(path, 'rb').read())
            part.add_header('Content-Disposition', 'attachment', filename=txt_name)
            msgRoot.attach(part)

        if ".mp3" in path:
            # mp3类型附件
            mp3_name = path.split("\\")[-1]
            part = MIMEApplication(open(path, 'rb').read())
            part.add_header('Content-Disposition', 'attachment', filename=mp3_name)
            msgRoot.attach(part)

        if ".zip" in path:
            # mp3类型附件
            zip_name = path.split("\\")[-1]
            part = MIMEApplication(open(path, 'rb').read())
            part.add_header('Content-Disposition', 'attachment', filename=zip_name)
            msgRoot.attach(part)

    smtp.sendmail(sender_mail, receiver, msgRoot.as_string())
    smtp.quit()
